//
//  GYShopHomeViewController.m
//  GoodLuck
//
//  Created by apple on 2018/2/1.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYShopHomeViewController.h"
#import "GYNaviView.h"
#import "GYShopMainViewController.h"
#import "GYShopDetailsViewController.h"
#import "GYShopEvaluateViewController.h"
#import "GYSearchViewController.h"
#import "GYBasePopoverViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"
#import "GYShopingCartViewController.h"
#import "GYUser.h"


@interface GYShopHomeViewController ()<GYNaviViewDelagate>

/** 导航栏View */
@property (nonatomic, weak) GYNaviView *naviView;

/** 导航栏左侧按钮 */
//@property (nonatomic, weak) UIButton *leftBtu;

/** 导航栏右侧按钮 */
//@property (nonatomic, weak) UIButton *rightBtu;

/** 当前正在显示的view */
@property (nonatomic, strong) UIViewController *showingVC;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;


@end

@implementation GYShopHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //NSLog(@"goods_id=======%@",self.goods_id);
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(rightBarButtonClick) button:nil];
    
    //初始化导航栏
    [self setupNaviView];
    
    //初始化所有子控制器
    [self setupChildersController];
}

- (void)setupNaviView {
    GYNaviView *naviView = [[[NSBundle mainBundle] loadNibNamed:@"GYNaviView" owner:nil options:nil] firstObject];
    naviView.frame = CGRectMake(0, 0, ScreenWidth, 44);
    naviView.delegate = self;
    self.navigationItem.titleView = naviView;
    self.naviView = naviView;
    
}

- (void)setupChildersController {
    GYShopMainViewController *mainVC = [[GYShopMainViewController alloc] init];
    mainVC.goods_id = self.goods_id;
    __weak typeof(self) weakSelf = self;

    //切换页面
    mainVC.jumpEvaluateViewCallBack = ^(NSInteger index){
        //移除其他控制器的View
        [weakSelf.showingVC.view removeFromSuperview];
        
        //添加新的控制器的View
        weakSelf.showingVC = weakSelf.childViewControllers[index];
        weakSelf.showingVC.view.frame = weakSelf.view.bounds;
        [weakSelf.view insertSubview:weakSelf.showingVC.view atIndex:0];

        //weakSelf.navigationController.navigationBar.translucent = NO;
        [weakSelf.naviView updateMarkLable:index+1];
    };
    
    GYShopDetailsViewController *detilesVC = [[GYShopDetailsViewController alloc] init];
    detilesVC.goods_id = self.goods_id;
    
    GYShopEvaluateViewController *evaluateVC = [[GYShopEvaluateViewController alloc] init];
    evaluateVC.goods_id = self.goods_id;
    
    [self addChildViewController:mainVC];
    [self addChildViewController:detilesVC];
    [self addChildViewController:evaluateVC];
    
    self.showingVC = mainVC;
    self.showingVC.view.frame = self.view.bounds;
    [self.view addSubview:self.showingVC.view];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"frame==============%@",NSStringFromCGRect(self.showingVC.view.frame));
    NSLog(@"view.frame==============%@",NSStringFromCGRect(self.view.frame));
}

#pragma mark -- GYNaviViewDelagate

- (void)naviBtuClick:(NSInteger)tag {
    
    //移除其他控制器的View
    [self.showingVC.view removeFromSuperview];
    
    //添加新的控制器的View
    
    self.showingVC = self.childViewControllers[tag-1];
    self.showingVC.view.frame = self.view.bounds;
    [self.view insertSubview:self.showingVC.view atIndex:0];
}


#pragma mark -- 导航栏按钮点击事件

- (void)rightBarButtonClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [weakSelf.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYShopingCartViewController *shopingCartVC = [[GYShopingCartViewController alloc] initWithNibName:@"GYShopingCartViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:shopingCartVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                break;
                
            case 3:
            {
                weakSelf.tabBarController.selectedIndex = 2;
                [weakSelf.tabBarController.tabBar setHidden:YES];
            }
                break;
                
            case 4:
            {
                if ([GYUser loadUserInfo] != nil) {
                    GYSystemMessageViewController *systemMsgVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:systemMsgVC animated:YES];
                }else {
                    weakSelf.tabBarController.selectedIndex = 2;
                    [weakSelf.tabBarController.tabBar setHidden:YES];
                }
            }
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"购物车",@"我的商城",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon3",@"ff_icon4",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}


- (void)dealloc {
    NSLog(@"GYShopHomeViewController=====dealloc");
}

@end
